package edu.jkd.service;

import edu.jkd.utils.QiniuUtils;
import edu.jkd.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;

public interface PictureService {
    String upload(MultipartFile file);


    @Service
    public class PictureServiceImpl implements PictureService {

        @Autowired
        private QiniuUtils qiniuUtils;

        @Override
        public String upload(MultipartFile file) {
            if (file.isEmpty()) {
                return "上传文件为空,请重试...";
            }
            String fileName = StringUtils.getRandomImgName(Objects.requireNonNull(file.getOriginalFilename()));
            try {
                String path = qiniuUtils.upload(file.getBytes(), fileName);

                // System.out.println("fileName           "+fileName);   随机创建得文件名
                // System.out.println("path为：               "+path);   随机创建得文件名和地址整合
                return "http://" + path;
                // return new Result(HttpStatus.OK.value(),path);
            } catch (IOException e) {
                e.printStackTrace();
                return "服务器内部错误...";
            }
        }

    }
}
